package com.chuantou.yuba.ui.fragment;

import java.util.HashMap;
import java.util.List;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;

import com.android.volley.Request.Method;
import com.android.volley.Response.ErrorListener;
import com.android.volley.Response.Listener;
import com.baidu.location.BDLocation;
import com.chuantou.yuba.R;
import com.chuantou.yuba.YBApp;
import com.chuantou.yuba.adapter.FishingShopAdapter;
import com.chuantou.yuba.api.UriHelper;
import com.chuantou.yuba.api.request.BaseGsonRequest;
import com.chuantou.yuba.api.request.GsonRequest;
import com.chuantou.yuba.api.to.FishingShopDataTO;
import com.chuantou.yuba.api.to.FishingShopTO;
import com.chuantou.yuba.ui.FishingShopDetailActivity;
import com.chuantou.yuba.ui.WebViewActivity;
import com.chuantou.yuba.ui.widget.LoadingView.Style;
import com.chuantou.yuba.util.Constants;

public class NearFishingShopFragment extends NearFilterFragment<FishingShopDataTO> {

	private BDLocation mLocation;
	private FishingShopAdapter mAdapter;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mLocation = YBApp.get().getLocation();
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View contentView = super.onCreateView(inflater, container, savedInstanceState);
		if (mAdapter == null) {
			mAdapter = new FishingShopAdapter(mContext);
			mAdapter.setShowDistance(false);
			mListView.setAdapter(mAdapter);
			mListView.setOnItemClickListener(new OnItemClickListener() {

				@Override
				public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
					Object obj = parent.getItemAtPosition(position);
					if (obj != null && obj instanceof FishingShopTO) {
						FishingShopTO shopTO = (FishingShopTO) obj;
						// 网店直接跳转到店铺
						if (shopTO.getTypeId() == FishingShopTO.TYPE_WANGDIAN) {
							String shopUrl = shopTO.getShopUrl();
							Intent intent = new Intent(getActivity(), WebViewActivity.class);
							Uri uri = Uri.parse(shopUrl);
							String scheme = uri.getScheme();
							String url = uri.toString();
							if (TextUtils.isEmpty(scheme)) {
								url = "http://" + url;
							}
							intent.putExtra(Constants.EXTRA_URL, url);
							intent.putExtra(Constants.EXTRA_TITLE, shopTO.getName());
							startActivity(intent);
						} else {
							Intent intent = new Intent(getActivity(), FishingShopDetailActivity.class);
							intent.putExtra(Constants.EXTRA_DATA, shopTO.getId());
							startActivity(intent);
						}
					}
				}
			});
			startRequest();
		}
		return contentView;
	}

	@Override
	protected BaseGsonRequest<FishingShopDataTO> getRequest(Listener<FishingShopDataTO> listener, ErrorListener errorListener) {
		String url = UriHelper.getNearFishingShopUrl();
		HashMap<String, String> params = new HashMap<String, String>();
		params.put(UriHelper.PARAMS_DISTANCE, String.valueOf("0"));
		params.put(UriHelper.PARAMS_PAGE_SIZE, String.valueOf(Constants.DEFAULT_PAGE_SIZE));
		params.put(UriHelper.PARAMS_PAGE_NO, String.valueOf(1));
		try {
			params.put(UriHelper.PARAMS_POINT, mLocation.getLongitude() + "," + mLocation.getLatitude());
		} catch (Exception e) {
			e.printStackTrace();
		}
		// 默认经销商
		params.put(UriHelper.PARAMS_TYPE, "1");
		GsonRequest<FishingShopDataTO> request = new GsonRequest<FishingShopDataTO>(Method.POST, url, listener, errorListener, params, FishingShopDataTO.class);
		return request;
	}

	@Override
	protected void clearAll() {
		mAdapter.clear();
		mAdapter.notifyDataSetChanged();
	}

	@Override
	public void onResponse(FishingShopDataTO response) {
		if (getActivity() != null) {
			super.onResponse(response);
			if (response != null) {
				List<FishingShopTO> shops = response.getShops();
				// 第一页
				if (response.getCurrentPage() <= 1) {
					// 保存到本地数据库缓存
					mAdapter.clear();
				}
				mAdapter.add(shops);
			} else {
				mLoadingView.setStyle(Style.EMPTY, getEmptyString());
			}
			mAdapter.notifyDataSetChanged();
		}
	}

	@Override
	protected String getEmptyString() {
		return mContext.getString(R.string.no_content);
	}

}
